/*采销管理----------------Caesar-------------------------------------------------------------------*/

/*加载分级联查功能选项*/
function goods_listCategoryP(categoryLevel) {
    var categoryName = $("#classP1").val();
    var user_data = {
        categoryLevel: categoryLevel,
        categoryName: $("#classP1").val(),
    }
    var succCallBack = function(data, status, response) {
        console.log(data)
        if(data.errorCode == "0") {
            if(data.data == null) {
                classStr = "<li>无数据</li>"
                $("#classUlP1").html(classStr);
            } else {
                var classStr = "";
                for(var i = 0; i < data.data.length; i++) {
                    classStr += "<li onclick='nextClassP(\"" + data.data[i].category_id + "\",\"" + data.data[i].category_code + "\",\"" + data.data[i].category_level + "\",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                }
                $("#classUlP1").html(classStr);
            }
        }
    }
    web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
}

//获取光标
$("#classP1").focus(function() {
    $("#classUlP1").show();
    goods_listCategoryP(1);
});
$(document).bind("click", function(e) {
    //id为menu的是菜单，id为open的是打开菜单的按钮
    if($(e.target).closest("#classUlP1").length == 0 && $(e.target).closest("#classP1").length == 0) {
        //点击id为menu之外且id不是不是open，则触发
        $("#classUlP1").hide()
    }
});

// 2,3,4级的模糊查询
function getNext_inputP(level, category_id) {
    console.log(level)
    if(level < 4) {
        $("#classP" + level + "")[0].oninput = function() {
            var user_data = {
                parentCateId: category_id,
                categoryName: $("#classP" + level + "").val()
            }
            var succCallBack = function(data, status, response) {
                console.log(data)
                if(data.errorCode == 0) {
                    if(data.data == null) {
                        classStr = "<li>无数据</li>";
                    } else {
                        var classStr = "";
                        for(var i = 0; i < data.data.length; i++) {
                            classStr += "<li onclick='nextClassP(\"" + data.data[i].category_id + "\",\"" + data.data[i].category_code + "\",\"" + data.data[i].category_level + "\",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                        }
                    }
                    if(data.data[0] == undefined) {
                        classValue = category_id;
                        var nextLevel = level + 1;
                        $("#classUlP" + nextLevel + "").html(classStr);
                        $("#classUlP" + level + "").hide();
                    } else {
                        var nextLevel = data.data[0].category_level;
                        $("#classUlP" + nextLevel + "").html(classStr);
                        $("#classUlP" + level + "").show();
                    }
                }
            }
            web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
        }
        //获取光标
        $("#classP" + level + "").focus(function() {
            $("#classUlP" + level + "").show();
        });
        $(document).bind("click", function(e) {
            //id为menu的是菜单，id为open的是打开菜单的按钮
            if($(e.target).closest("#classUlP" + level + "").length == 0 && $(e.target).closest("#classP" + level + "").length == 0) {
                //点击id为menu之外且id不是不是open，则触发
                $("#classUlP" + level + "").hide()
            }
        });
    }

}
var category_id_of_search = ""; //定义供搜索的分类id
var category_code_of_search1 = ""; //定义供搜索的分类code
var category_code_of_search2 = ""; //定义供搜索的分类code
var category_code_of_search3 = ""; //定义供搜索的分类code
function nextClassP(category_id,category_code,level, category_name) {
    $("#classP" + level + "").val(category_name)
    if(level == 1) {
        $("#classP2").val("");
        $("#classP3").val("");
        category_code_of_search1 = category_code;
        category_id_of_search = category_id;
    } else if(level == 2) {
        $("#classP3").val("");
        category_code_of_search2 = category_code;
        category_id_of_search = category_id;
    } else if(level == 3) {
        category_code_of_search3 = category_code;
        category_id_of_search = category_id;
    }
    var user_data = {
        parentCateId: category_id
    }
    var succCallBack = function(data, status, response) {
        console.log(data)
        if(data.errorCode == "0") {
            if(data.data == null) {
                classStr = "<li>无数据</li>";
            } else {
                var classStr = "";
                for(var i = 0; i < data.data.length; i++) {
                    classStr += "<li onclick='nextClassP(\"" + data.data[i].category_id + "\",\"" + data.data[i].category_code + "\",\"" + data.data[i].category_level + "\",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                }
            }
            if(data.data[0] == undefined) {
                classValue = category_id;
                var nextLevel = level + 1;
                $("#classUlP" + nextLevel + "").html(classStr);
                $("#classUlP" + level + "").hide();
            } else {
                var nextLevel = data.data[0].category_level;
                $("#classUlP" + nextLevel + "").html(classStr);
                $("#classUlP" + nextLevel + "").show();
                $("#classUlP" + level + "").hide()
            }
        }
    }
    getNext_inputP(level + 1, category_id);
    web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
}

function cx_list() {
    if($("#classP1").val() != "" && $("#classP2").val() != "" && $("#classP3").val() == ""){
        var category_code_of_search = category_code_of_search2;
    }else if($("#classP1").val() != "" && $("#classP2").val() == "" && $("#classP3").val() == ""){
        var category_code_of_search = category_code_of_search1;
    }else if($("#classP1").val() == "" && $("#classP2").val() == "" && $("#classP3").val() == ""){
        var category_code_of_search = "";
    }else {
        var category_code_of_search = category_code_of_search3;
    }
    $.ajax({
        contentType: "application/json",
        type: "POST",
        url: root.goodsInter.goods_getLowLevelCategory,
        cache: false, //禁用缓存
        dataType: "json",
        data: JSON.stringify({
            "pageSize": 20,
            "nowPage": 1,
            "model": {
                "categoryId": category_id_of_search,
                "categoryCode": category_code_of_search,
                "beginTime": $("#timemin").val(),
                "endTime": $("#timemax").val(),
                "categoryName": $("#code_name").val(),
                "buyName": $("#purchase_man").val(),
                "salesManName": $("#operate_man").val(),
            }
        }),
        success: function(result) {
            cxList(result);
            $('.M-box4').pagination({
                pageCount: result.data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function(api) {
                    $.ajax({
                        contentType: "application/json",
                        type: "POST",
                        url: root.goodsInter.goods_getLowLevelCategory,
                        cache: false, //禁用缓存
                        dataType: "json",
                        data: JSON.stringify({
                            "pageSize": 20,
                            "nowPage": api.getCurrent(),
                            "model": {
                                "categoryId": category_id_of_search,
                                "beginTime": "",
                                "endTime": "",
                                "categoryName": $("#code_name").val(),
                                "buyName": $("#purchase_man").val(),
                                "salesManName": $("#operate_man").val(),
                            }
                        }),
                        success: function(result) {
                            cxList(result);
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}
/*采销表格渲染*/
var cxdataSource = new Array();
//table的数据源
var table; //table对象
cxdataSource = [];

function cxList(data) {
    console.log(data)
    cxdataSource.splice(0, cxdataSource.length); //清空数组
    $.each(data.data.list, function(i, field) {
        var rowData = new Array();
        rowData.push(field.category_name);
        rowData.push(field.buy_name);
        rowData.push(field.sale_name);
        rowData.push(field.last_modified_date);
        rowData.push(field.buy_department);
        rowData.push(field.sale_department);
        rowData.push(field.category_code);

        rowData.push(field.category_his_id);
        rowData.push(field.category_id);
        rowData.push(field.parent_cate_id);
        rowData.push(field.buyer_id);
        rowData.push(field.salesman_id);
        rowData.push(field.category_level);
        cxdataSource.push(rowData);
    });
    initDataTable(cxdataSource);

    function initDataTable(cxdataSource) {
        var dttable = $('#cxdatatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#cxdatatable").DataTable({
            "order": [
                [3, "desc"]
            ],
            "autoWidth": false,
            "ordering": true,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />",
            },
            data: cxdataSource,
            columnDefs: [{
                "visible": true,
                "targets": 0
            },
                {
                    "render": function(data, type, row) {
                        if(row[0] != null && row[6] != null) {
                            var val = '<span>' + row[6] + '</span> <span>' + row[0] + '</span>';
                        } else if(row[0] == null && row[6] == null) {
                            var val = '<span></span> <span></span>';
                        } else {
                            if(row[6] == null) {
                                var val = '<span></span> <span>' + row[0] + '</span>';
                            } else if(row[0] == null) {
                                var val = '<span>' + row[6] + '</span> <span></span>';
                            }
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function(data, type, row) {
                        if(row[1] != null && row[4] != null) {
                            var val = '<span>' + row[4] + '</span> <span>' + row[1] + '</span><a onclick="updataManager(\''+row+'\',1)" class="Hui-iconfont f-r icon-edit powerButton 404">&#xe6df;</a>';
                        } else if(row[1] == null && row[4] == null) {
                            var val = '<span></span> <span></span><a onclick="updataManager(\''+row+'\',1)" class="Hui-iconfont f-r icon-edit powerButton 404">&#xe6df;</a>';
                        } else {
                            if(row[4] == null) {
                                var val = '<span></span> <span>' + row[1] + '</span><a onclick="updataManager(\''+row+'\',1)" class="Hui-iconfont f-r icon-edit powerButton 404">&#xe6df;</a>';
                            } else if(row[1] == null) {
                                var val = '<span>' + row[4] + '</span> <span></span><a onclick="updataManager(\''+row+'\',1)" class="Hui-iconfont f-r icon-edit powerButton 404">&#xe6df;</a>';
                            }
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function(data, type, row) {
                        if(row[2] != null && row[5] != null) {
                            var val = '<span>' + row[5] + '</span> <span>' + row[2] + '</span><a onclick="updataManager(\''+row+'\',2)" class="Hui-iconfont f-r icon-edit powerButton 404">&#xe6df;</a>';
                        } else if(row[2] == null && row[5] == null) {
                            var val = '<span></span> <span></span><a onclick="updataManager(\''+row+'\',2)" class="Hui-iconfont f-r icon-edit powerButton 404">&#xe6df;</a>';
                        } else {
                            if(row[5] == null) {
                                var val = '<span></span> <span>' + row[2] + '</span><a onclick="updataManager(\''+row+'\',2)" class="Hui-iconfont f-r icon-edit powerButton 404">&#xe6df;</a>';
                            } else if(row[2] == null) {
                                var val = '<span>' + row[5] + '</span> <span></span><a onclick="updataManager(\''+row+'\',2)" class="Hui-iconfont f-r icon-edit powerButton 404">&#xe6df;</a>';
                            }
                        }
                        return val;
                    },
                    "targets": 2
                },
            ]
        });
        power();
    };
}
/*修改负责人*/
function updataManager(data,id) {
    var cxArr= data.split(",");
    console.log(cxArr)
    //更改页面缓存
    var editData = {
        id:id,
        categoryCode:cxArr[6],
        category_name:cxArr[0],

        categoryHisId:cxArr[7],
        categoryId:cxArr[8],
        parentId:cxArr[9],
        buyId:cxArr[10],
        salesId:cxArr[11],
        categoryLevel:cxArr[12],
    };
    window.session_storage.setItem('editData',JSON.stringify(editData));
    layer_show("配置负责人", "updataManager.html", "500", "400");
}
/*采销----------------Caesar-------------end------------------------------------------------------*/